package com.yyh2;

import com.yyh3.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class Test1 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //创建数组
        ArrayList<Student> list = new ArrayList<>();
        //循环
        while (true){
            //输出
            System.out.println("-------欢迎来到学生管理系统--------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");

            int choice = sc.nextInt();

            switch (choice) {
                case 1:
                    addStudent(list);
                    break;
                case 2:
                    deleteStudent(list);
                    break;
                case 3:
                    updateStudent(list);
                    break;
                case 4:
                    queryStudents(list);
                    break;
                case 5:
                    System.out.println("感谢您的使用");
                    break;
                default:
                    System.out.println("您的输入有误");
                    break;
            }
        }
    }
    private static void updateStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要修改的学号:");
        String updateSid = sc.nextLine();

        int index = getIndex(list, updateSid);

        if(index == -1){
            System.out.println("该信息不存在, 请重新输入");
        }else{
            System.out.println("请输入新的姓名:");
            String name = sc.nextLine();
            System.out.println("请输入新的年龄:");
            String age = sc.nextLine();
            System.out.println("请输入新的生日:");
            String birthday = sc.nextLine();

            Student stu = new Student(updateSid,name,age,birthday);

            list.set(index, stu);

            System.out.println("修改成功!");
        }
    }
    private static void deleteStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要删除的学号:");
        String delSid = sc.nextLine();

        int index = getIndex(list, delSid);

        if(index == -1){
            System.out.println("该信息不存在, 请重新输入");
        }else{
            list.remove(index);
            System.out.println("删除成功!");
        }

    }

    public static int getIndex(ArrayList<Student> list, String sid){
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if(sid.equals(stu.getSid())){
                index = i;
            }
        }
        return index;
    }

    public static void queryStudents(ArrayList<Student> list) {
        if (list == null || list.size() == 0) {
            System.out.println("无学生信息，请添加后重试");
        } else {
            System.out.println("学号\t\t姓名\t年龄\t生日");
            for (int i = 0; i < list.size(); i++) {
                Student stu = list.get(i);
                System.out.println(stu.getSid() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t\t" + stu.getBirthday());
            }
        }
    }

    public static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);

        String sid;

        while(true){
            System.out.println("请输入学号:");
            sid = sc.nextLine();
            int index = getIndex(list, sid);
            if(index == -1){
                break;
            }else{
                System.out.println("学号已被占用");
            }
        }

        System.out.println("请输入姓名:");
        String name = sc.nextLine();
        System.out.println("请输入年龄:");
        String age = sc.nextLine();
        System.out.println("请输入生日:");
        String birthday = sc.nextLine();

        Student stu = new Student(sid, name, age, birthday);

        list.add(stu);

        System.out.println("添加学生成功");
    }
}
